package com.lancoo.znbkxx.teacher.ui.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ClassStuGradeDistributionInfor
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.aibk_adapter_item_estimate_grade_student.view.*


class AibkEstimateGradeStuIntervalAdapter(
        val context: Context?,
        val classStuGradeDistributionInfor: ClassStuGradeDistributionInfor
): KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        var view:View? = null
        if(position == 0){
            view = LayoutInflater.from(context)
                    .inflate(R.layout.aibk_adapter_item_estimate_grade_student_title, parent, false)
        }else {
            view = LayoutInflater.from(context)
                    .inflate(R.layout.aibk_adapter_item_estimate_grade_student, parent, false)
        }
        return view;
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        if(position > 0){
            val stuName = classStuGradeDistributionInfor.ClassScoreTable[position-1].StuName
            val stuID = classStuGradeDistributionInfor.ClassScoreTable[position-1].StuID
            val stuScore = classStuGradeDistributionInfor.ClassScoreTable[position-1].StuScore
            var scoreChange = classStuGradeDistributionInfor.ClassScoreTable[position-1].ScoreChange

            holder.convertView.TvStuName.text = stuName
            holder.convertView.TvStuID.text = stuID
            holder.convertView.TvStuScore.text = LancooMethod.killlingDouble(stuScore)
            if(scoreChange>0){
                holder.convertView.TvStuScoreChange.text= "+ "+LancooMethod.killlingDouble(Math.abs(scoreChange))
            }else if(scoreChange == 0.0){
                holder.convertView.TvStuScoreChange.text= "  "+LancooMethod.killlingDouble(Math.abs(scoreChange))
            }else{
                holder.convertView.TvStuScoreChange.text= "- "+LancooMethod.killlingDouble(Math.abs(scoreChange))
            }

            holder.convertView.TvStuName.setTextColor(context!!.resources.getColor(R.color.textColor2))
            holder.convertView.TvStuID.setTextColor(context!!.resources.getColor(R.color.textColor2))
            holder.convertView.TvStuScore.setTextColor(context!!.resources.getColor(R.color.textColor2))
            holder.convertView.TvStuScoreChange.setTextColor(context!!.resources.getColor(R.color.textColor2))

            var drawable:Drawable? = null
            if(scoreChange>0) {
                 drawable = context.resources.getDrawable(R.mipmap.aibk_icon_score_up)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
            }else if(scoreChange==0.0) {
                 drawable = null
            }else{
                 drawable = context.resources.getDrawable(R.mipmap.aibk_icon_score_down)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
            }

            holder.convertView.TvStuScoreChange.setCompoundDrawables(null, null, drawable, null)
        }
    }

    override fun getItemCount(): Int {
        val classScoreChangeSize = classStuGradeDistributionInfor.ClassScoreTable.size
        return classScoreChangeSize+1
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

}